package com.zhao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhao.bean.Category;
import com.zhao.bean.PageParam;
import com.zhao.dao.CategoryDao;
import com.zhao.service.CategoryService;
import com.zhao.service.SetmealService;
import com.zhao.service.dishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class CategoryControllerImpl implements CategoryService {

    @Autowired
    private CategoryDao dao;

    @Autowired
    private dishService dishService;

    @Autowired
    private SetmealService setmealService;

    //添加
    @Override
    public int add(Category category) {

        return dao.insert(category);
    }

    //分页查询
    @Override
    public IPage<Category> find(PageParam pageParam) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //设置排序，按升序
        wrapper.orderByAsc(Category::getSort, Category::getName);

        Page<Category> page = new Page<>(pageParam.getPage(), pageParam.getPageSize());

        return dao.selectPage(page, wrapper);
    }

    //删除
    @Override
    public int delete(long id) {
        //调用Service查询分类下面是否还有数据，如果有就不能删除
        long dish = dishService.findDish(id);
        System.out.println("dish::::::::"+dish);
        long setmea = setmealService.findSetmea(id);
        System.out.println("setmea::::::::"+setmea);
        if (dish == 0 && setmea == 0) {
            return dao.deleteById(id);
        }else {
            return 0;
        }
    }
    //修改
    @Override
    public int update(Category category) {

        return dao.updateById(category);
    }

    @Override
    public List<Category> locating(int type) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getType,type);

        return dao.selectList(wrapper);
    }


}
